//
//  DYMSDingzuorenViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYMSDingzuorenViewController: SYBaseViewController {

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        return v
    }()
    
    var secBgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        return v
    }()
    
    let info: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 15.autoFontSize()
        
        return v
    }()
    let xiugai: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0xFC6435)
        v.font = 14.autoFontSize()
        v.text  = "修改"
        return v
    }()
    let imgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_bianji")
        return v
    }()
    
    var bianji: UIButton = {
        let v = UIButton()
        return v
    }()
    
    let nameImgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_ren")
        return v
    }()
    
    let nameTf: UITextField = {
        let v:UITextField = UITextField()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 15.autoFontSize()
        v.placeholder = "请输入姓名"
        return v
    }()
    
    lazy var phone: VGBaseTitleImgView = {
        let v = VGBaseTitleImgView(frame: CGRect.zero, type: .imgLeft, showType: .byContent, imgSuperViewMargin: 0, imgTitleMargin: 11)
        v.imageV.sy_name("meishi_phone")
        v.textLb.text = "+86"
        v.textLb.textColor = UIColor(hex: 0x333333)
        v.textLb.font = 15.autoFontSize()
        v.tapClosure = {[weak self] (index, select) in
            if let `self` = self {
                
            }
        }
        return v
    }()
    
    let beizhuImgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_beizhu")
        return v
    }()
    
    let beizhuTf: SYBaseTextView = {
        let v:SYBaseTextView = SYBaseTextView()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 15.autoFontSize()
        v.backgroundColor = .clear
        return v
    }()
    let beizhuLb: UILabel = {
        let v = UILabel()
        v.text = "留言备注，我们将尽量安排"
        v.font = 15.autoFontSize()
        v.textColor = UIColor(hex: 0xD1D1D1)
        v.isUserInteractionEnabled = true

        return v
    }()
    
    let phoneTf: UITextField = {
        let v:UITextField = UITextField()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 15.autoFontSize()
        v.placeholder = "请输入电话"
        return v
    }()
    
    let nv: UILabel = {
        let v = UILabel()
        v.text = "女士"
        v.font = 14.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        v.isUserInteractionEnabled = true
        return v
    }()
    let nan: UILabel = {
        let v = UILabel()
        v.text = "先生"
        v.font = 14.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        v.isUserInteractionEnabled = true
        return v
    }()
    let nvImgView: UIImageView = {
        let v = UIImageView()
        v.isUserInteractionEnabled = true
        return v
    }()
    let nanImgView: UIImageView = {
        let v = UIImageView()
        v.isUserInteractionEnabled = true
        return v
    }()
    
    var nvBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    var nanBt: UIButton = {
        let v = UIButton()
        return v
    }()

    
    var sepView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0x999999)
        return v
    }()
    
    let limitNum: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 14.autoFontSize()
        v.text  = "0/25"
        return v
    }()
    
    let lineView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let secLineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex:0xE5E5E5)
        return v
    }()
    
    let xialaImgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_xiala")
        return v
    }()
    var xialaBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    let bottom: DYMSDingzuorenBottom  = DYMSDingzuorenBottom()
    
    var orderParams: [String: String] = [:]
    
    init(params: [String: String], title: String) {
        super.init(nibName: nil, bundle: nil)
        self.title = title
        
        info.text  = params["showStr"]

        self.orderParams = params
        if params["sex"] == "1" {
            nvImgView.image = UIImage(named: "meishi_select")
            nanImgView.image = UIImage(named: "meishi_select_select")
            self.orderParams["sex"] = "1"
        } else {
            nvImgView.image = UIImage(named: "meishi_select_select")
            nanImgView.image = UIImage(named: "meishi_select")
            self.orderParams["sex"] = "0"
        }
        orderParams.removeValue(forKey: "showStr")
        orderParams.removeValue(forKey: "renshu")
        orderParams.removeValue(forKey: "day")
        orderParams.removeValue(forKey: "riqi")
        orderParams.removeValue(forKey: "isPrivate")
        orderParams.removeValue(forKey: "shijian")
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Constant.bgViewColor
        
        initViews()
    }
    
    func getOrder() {
        
        if let v = self.orderParams["contacts"] {
            if v.isEmpty {
                self.sy_toast("请填写姓名")
                return
            }
        } else {
            self.sy_toast("请填写姓名")
            return

        }
        if let v = self.orderParams["phone"] {
            if v.isEmpty {
                self.sy_toast("请填写联系人电话")
                return
            }
            if !v.checkIsMobileNum() {
                self.sy_toast("请填写正确手机号")
                return
            }
        } else {
            self.sy_toast("请填写联系人电话")
            return
        }
        
        requestVM.baseRequest(disposeBag: dispose, type: .dy_meishiOrder(dic: self.orderParams), modelClass: SGBaseModel<SGSimpleModel>.self) {[weak self] (res) in
            if let `self` = self {
                let vc = DYMSDaiquerenViewController(id:res.data?.id ?? "")
                self.sy_push(vc)
            }

        } Error: {

        }
    }
    
    func initViews() {
        
        bottom.clickedClosure = {[weak self] _ in
            if let `self` = self {
//                let vc = DYMSDaiquerenViewController(id:"15050")//res.data?.id ?? "")
//                self.sy_push(vc)
                self.getOrder()
            }
        }
        
        //当文本框内容改变时，将内容输出到控制台上
        nameTf.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {[weak self] in
                if let `self` = self {
                    self.passParamsClosure?($0)
                    self.orderParams["contacts"] = $0
                }
                print("您输入的是：\($0)")
            })
            .disposed(by: dispose)
        phoneTf.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {[weak self] in
                if let `self` = self {
                    self.passParamsClosure?($0)
                    self.orderParams["phone"] = $0
                }
                print("您输入的是：\($0)")
            })
            .disposed(by: dispose)
        beizhuTf.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {[weak self] in
                if let `self` = self {
                    self.passParamsClosure?($0)
                    if self.beizhuTf.text == "" {
                        self.beizhuLb.isHidden = false
                    } else {
                        
                        self.beizhuLb.isHidden = true
                    }
                    if $0.count > 25 {
                        let substr = self.beizhuTf.text.prefix(25)
                        self.beizhuTf.text = String(substr)
                    }
                    self.orderParams["remarks"] = $0
                }
                print("您输入的是：\($0)")
            })
            .disposed(by: dispose)
        
        bianji.addAction {[weak self] in
            if let `self` = self {
                self.popVC()
            }
        }
        nanBt.addAction {[weak self] in
            if let `self` = self {
                self.nvImgView.sy_name("meishi_select")
                self.nanImgView.sy_name("meishi_select_select")
                self.orderParams["sex"] = "1"
            }
        }
        nvBt.addAction {[weak self] in
            if let `self` = self {
                self.nanImgView.sy_name("meishi_select")
                self.nvImgView.sy_name("meishi_select_select")
                self.orderParams["sex"] = "0"
            }
        }
        xialaBt.addAction {[weak self] in
            if let `self` = self {
                
            }
        }
        
        view.addSubview(bottom)
        
        view.addSubview(bgView)
        bgView.addSubview(info)
        bgView.addSubview(xiugai)
        bgView.addSubview(imgView)
        bgView.addSubview(bianji)
        
        bottom.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(49.autoWidth()+BOTTOM_SAFE_HEIGHT)
        }
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth() + NAV_HEIGHT)
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(44.autoWidth())
        }
        info.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(40.autoWidth())
            make.right.equalToSuperview().offset(-60.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        xiugai.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        imgView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(13.autoWidth())
            make.width.height.equalTo(16.autoWidth())
        }
        bianji.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(secBgView)
        secBgView.addSubview(nameImgView)
        secBgView.addSubview(nameTf)
        secBgView.addSubview(nan)
        secBgView.addSubview(nv)
        secBgView.addSubview(nanImgView)
        secBgView.addSubview(nvImgView)
        secBgView.addSubview(nanBt)
        secBgView.addSubview(nvBt)
        secBgView.addSubview(phone)
        secBgView.addSubview(phoneTf)
        secBgView.addSubview(sepView)
        secBgView.addSubview(secLineView)
        secBgView.addSubview(beizhuImgView)
        secBgView.addSubview(limitNum)
        secBgView.addSubview(lineView)
        secBgView.addSubview(xialaImgView)
        secBgView.addSubview(xialaBt)
        secBgView.addSubview(beizhuLb)
        secBgView.addSubview(beizhuTf)
        secBgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(69.autoWidth() + NAV_HEIGHT)
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
//            make.bottom.equalToSuperview()
        }
        nameImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.left.equalToSuperview().offset(12.autoWidth())
            make.width.height.equalTo(16.autoWidth())
        }
        nameTf.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.left.equalToSuperview().offset(40.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        nan.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(18.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        nv.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(18.autoWidth())
            make.right.equalToSuperview().offset(-83.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        nanImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14.autoWidth())
            make.right.equalToSuperview().offset(-43.autoWidth())
            make.height.equalTo(27.autoWidth())
        }
        nvImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14.autoWidth())
            make.right.equalToSuperview().offset(-113.autoWidth())
            make.height.equalTo(27.autoWidth())
        }
        nanBt.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameTf)
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.left.equalTo(nanImgView)
            make.height.equalTo(20.autoWidth())
        }
        nvBt.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameTf)
            make.right.equalToSuperview().offset(-83.autoWidth())
            make.left.equalTo(nvImgView)
            make.height.equalTo(20.autoWidth())
        }
        phone.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(58.autoWidth())
            make.left.equalToSuperview().offset(12.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        phoneTf.snp.makeConstraints { (make) in
            make.centerY.equalTo(phone)
            make.left.equalToSuperview().offset(95.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.centerY.equalTo(phone)
            make.left.equalToSuperview().offset(87.autoWidth())
            make.height.equalTo(15.autoWidth())
            make.width.equalTo(1.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(44.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(0.5.autoWidth())
        }
        secLineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(88.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(0.5.autoWidth())
        }
        beizhuImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(103.autoWidth())
            make.left.equalToSuperview().offset(12.autoWidth())
            make.width.height.equalTo(16.autoWidth())
        }
        beizhuTf.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(97.autoWidth())
            make.left.equalToSuperview().offset(36.autoWidth())
            make.right.equalToSuperview().offset(-50.autoWidth())
            make.height.greaterThanOrEqualTo(30.autoWidth())
            make.bottom.equalToSuperview().offset(-15.autoWidth())
        }
        limitNum.snp.makeConstraints { (make) in
            make.centerY.equalTo(beizhuImgView)
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        xialaImgView.snp.makeConstraints { (make) in
            make.centerY.equalTo(phoneTf)
            make.left.equalToSuperview().offset(71.autoWidth())
            make.height.equalTo(5.autoWidth())
            make.width.equalTo(9.autoWidth())
        }
        xialaBt.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(58.autoWidth())
            make.left.equalToSuperview().offset(12.autoWidth())
            make.right.equalTo(sepView)
        }
        beizhuLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(103.autoWidth())
            make.height.equalTo(16.autoWidth())
            make.left.equalToSuperview().offset(40.autoWidth())
            
        }
    }
    
}
